package com.sunsharing.common.mailbox;

import com.sunsharing.common.Constant;
import com.sunsharing.common.Context;
import com.sunsharing.common.utils.StringUtils;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-4-3
 * Time: 下午2:56
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractMailbox implements Mailbox {

    public List<QueueHandle> list = new ArrayList<QueueHandle>();
    Map<String,Long> requestMonitor = new HashMap<String, Long>();
    Map<String,Long> resultMonitor = new HashMap<String, Long>();
    protected int length;
    @Override
    public void addQueueListener(QueueHandle queueHandle) {
        list.add(queueHandle);
    }

    protected void addOne(Context contect)
    {
        if(contect.getMsgType()== Constant.MSG_TYPE_REQUEST)
        {
            String hour = StringUtils.getCurrentDateHour();
            if(requestMonitor.get(hour)==null)
            {
                Long l = 0L;
                requestMonitor.put(hour,l);
            }
            long l = requestMonitor.get(hour);
            l++;
            requestMonitor.put(hour,l);
        }
        if(contect.getMsgType()==Constant.MSG_TYPE_RESULT)
        {
            String hour = StringUtils.getCurrentDateHour();
            if(resultMonitor.get(hour)==null)
            {
                Long l = 0L;
                resultMonitor.put(hour,l);
            }
            long l = resultMonitor.get(hour);
            l++;
            resultMonitor.put(hour,(l));
        }
    }

    public String monitorStr()
    {
        StringBuffer rst = new StringBuffer("\n目前待处理的队列长度:"+length+"\n");
        rst.append("  处理的请求任务数:---------------\n");
        for(Iterator<String> iter = requestMonitor.keySet().iterator();iter.hasNext();)
        {
            String key = iter.next();
            rst.append("  "+key+"-----"+requestMonitor.get(key)+"\n");
        }
        rst.append("  处理的结果任务数:----------------\n");
        for(Iterator<String> iter = resultMonitor.keySet().iterator();iter.hasNext();)
        {
            String key = iter.next();
            rst.append("  "+key+"-----"+resultMonitor.get(key)+"\n");
        }
        return rst.toString();
    }
}
